//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.neurosky.thinkgear;

import com.neurosky.thinkgear.FFT;
import com.neurosky.thinkgear.FFTResult;
import com.neurosky.thinkgear.HammingWindow;
import com.neurosky.thinkgear.PSDResult;

public class PSD {
    private FFT a;
    private FFTResult b;
    private HammingWindow c;
    private int d;
    private int e;
    private int f;
    private float g = 0.5F;
    private float[] h;
    private float[] i;
    private float[] j;
    private float[] k;
    private float[] l;
    private float m;
    private float n;
    private float[] o;
    private int p;
    private int q;
    private int r;
    private int s;

    public PSD(int var1, int var2, int var3) {
        this.p = var3;
        this.e = var1;
        this.q = var2;
        this.j = new float[this.e];
        this.l = new float[this.q / 2 + 1];
        this.c = new HammingWindow(this.e);
        this.h = this.c.generateCoeffs(this.e);
        this.m = this.a(this.h);
        this.o = new float[this.q / 2 + 1];

        for(this.r = 0; this.r <= this.q / 2; ++this.r) {
            this.o[this.r] = (float)var3 / (float)this.q * (float)this.r;
        }

        this.a = new FFT();
    }

    public PSDResult pWelch(float[] var1) {
        this.d = var1.length;
        if(this.e > this.d) {
            return new PSDResult(new float[]{-1.0F}, new float[]{-1.0F});
        } else {
            this.f = (this.d - (int)((float)this.e * this.g)) / (this.e - (int)((float)this.e * this.g));
            this.i = new float[this.q / 2 + 1];

            for(this.r = 0; this.r < this.f; ++this.r) {
                System.arraycopy(var1, this.e / 2 * this.r, this.j, 0, this.e);
                this.j = this.c.applyCoeffs(this.j);
                this.b = this.a.calculateFFT(this.j, this.l, 1, this.q);
                this.k = this.b.getPower();

                for(this.s = 0; this.s < this.q / 2 + 1; ++this.s) {
                    this.i[this.s] = (float)((double)this.i[this.s] + Math.pow((double)this.k[this.s], 2.0D));
                }
            }

            for(this.r = 1; this.r < this.i.length - 1; ++this.r) {
                this.i[this.r] *= 2.0F;
            }

            this.n = (float)(this.f * this.p) * this.m;

            for(this.r = 0; this.r < this.i.length; ++this.r) {
                this.i[this.r] /= this.n;
            }

            return new PSDResult(this.o, this.i);
        }
    }

    private float a(float[] var1) {
        float var2 = 0.0F;

        for(this.r = 0; this.r < this.e; ++this.r) {
            var2 = (float)((double)var2 + Math.pow((double)var1[this.r], 2.0D));
        }

        return var2;
    }
}
